<?php

namespace App\Http\Controllers;

use App\Banner;
use App\Banner2;
use App\Datatables;
use Illuminate\Http\Request;
use DB;

/**
 * Class BannerController
 * @package App\Http\Controllers
 */
class BannerController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * banner列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function bannerList(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::BannerList());
        }
        return view('banner.list');
    }

    /**
     * banner删除
     * @param Banner $banner
     * @return array
     * @throws \Exception
     */
    public function bannerDel(Banner $banner)
    {
        if (!$banner->delete()) {
            return ['status' => 2, 'message' => '删除失败'];
        }
        return ['status' => 1, 'message' => '删除成功'];
    }

    /**
     * 添加banner
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function bannerPublish(Request $request)
    {
        if ($request->isMethod('post')) {
            $return = Banner::BannerPublish($request);
            if ($return['status'] === 1) {
                return redirect('/banner/list')->with('status', $return['status'])->with('message', $return['message']);
            }
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('banner.bannerPublish');
    }

    /**
     * 修改banner
     * @param Request $request
     * @param Banner $banner
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function bannerModify(Request $request, Banner $banner)
    {
        if ($request->isMethod('post')) {
            $return = Banner::ModifyBanner($request, $banner);
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('banner.bannerModify')->with(compact('banner'));
    }


    /**
     * 商城banner列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function banner2List(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::Banner2List());
        }
        return view('banner.list2');
    }

    /**
     * 商城banner删除
     * @param Banner $banner
     * @return array
     * @throws \Exception
     */
    public function banner2Del(Banner2 $banner)
    {
        if (!$banner->delete()) {
            return ['status' => 2, 'message' => '删除失败'];
        }
        return ['status' => 1, 'message' => '删除成功'];
    }

    /**
     * 添加商城banner
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function banner2Publish(Request $request)
    {
        if ($request->isMethod('post')) {
            $return = Banner2::BannerPublish($request);
            if ($return['status'] === 1) {
                return redirect('/banner2/list')->with('status', $return['status'])->with('message', $return['message']);
            }
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('banner.banner2Publish');
    }

    /**
     * 修改商城banner
     * @param Request $request
     * @param Banner $banner
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function banner2Modify(Request $request, Banner2 $banner)
    {
        if ($request->isMethod('post')) {
            $return = Banner2::ModifyBanner($request, $banner);
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('banner.banner2Modify')->with(compact('banner'));
    }


    public function bannerPicList(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::BannerPicList());
        }

        return view('banner.bannerPic');
    }

    public function bannerPicAdd(Request $request)
    {
        if (!DB::table('banner_pic')->insert(['path' => $request->pic, 'title' => $request->title])) {
            return response()->json(['status' => 2, 'message' => '失败']);
        }

        return response()->json(['status' => 1, 'message' => '成功']);
    }
}
